package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.database.entity.ArenaGameConfigEntity
import com.promise.jokerdream.ui.components.CheckboxOptionRow
import com.promise.jokerdream.ui.components.RoundCheckbox
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.TimePickerDialog
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.ui.theme.AppSpacing

/**
 * 竞技场设置卡片
 */
@Composable
fun ArenaSettingCard(
    config: ArenaGameConfigEntity,
    onArenaChange: (Boolean) -> Unit,
    onScheduledChallengeChange: (Boolean) -> Unit,
    onRegistrationTimeChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "竞技场",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onArenaChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // 定时挑战选项
                    CheckboxOptionRow(
                        title = "定时挑战",
                        checked = config.scheduledChallengeEnabled,
                        onCheckedChange = onScheduledChallengeChange
                    )
                    
                    // 只有选中定时挑战时才显示时间选择
                    if (config.scheduledChallengeEnabled) {
                        ChallengeTimeSelector(
                            selectedTime = config.registrationTime,
                            onTimeChange = onRegistrationTimeChange
                        )
                    }
                }
            }
        }
    )
}

/**
 * 挑战时间选择器
 */
@Composable
private fun ChallengeTimeSelector(
    selectedTime: String,
    onTimeChange: (String) -> Unit
) {
    var showTimePicker by remember { mutableStateOf(false) }
    
    SelectionRow(
        label = "挑战时间：",
        selectedText = selectedTime,
        onClick = { showTimePicker = true }
    )
    
    // 时间选择器对话框
    TimePickerDialog(
        show = showTimePicker,
        onTimeSelected = { timeString ->
            onTimeChange(timeString)
            showTimePicker = false
        },
        onDismiss = { showTimePicker = false }
    )
}
